package team.first.movieapiv1pro.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv1pro.domain.Staff;
import team.first.movieapiv1pro.dto.RequestStaff;
import team.first.movieapiv1pro.service.StaffService;
import team.first.movieapiv1pro.vo.ApiResponse;
import team.first.movieapiv1pro.vo.ApiResponsePaging;
import team.first.movieapiv1pro.vo.ResponseInnerMovie;
import team.first.movieapiv1pro.vo.ResponseStaff;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv1pro.controller
 * @Date: 2021/11/25 13:11
 * @Description:
 */
@RestController
public class StaffController {
    @Autowired
    StaffService staffService;

    /**
     * 1.4 获取指定编号的影人信息
     * 接口地址:GET /api/v1/staff/{staffId}
     */
    @ApiOperation("1.4 GET: 获取指定编号的影人信息")
    @GetMapping("/api/v1/staff/{staffId}")
    public ApiResponse<ResponseStaff> actor(@ApiParam("员工编号")  @PathVariable("staffId") Integer staffId) {
        ApiResponse<ResponseStaff> apiResponse = new ApiResponse<>();

        // 如果数据库操作有异常则直接返回500
        try {
            Staff staff = staffService.getById(staffId);

            ResponseStaff responseStaff = BeanUtil.copyProperties(staff, ResponseStaff.class);

            List<ResponseInnerMovie> actedMovies = staffService.getAllMoviesByStaffId(staffId, 1);
            List<ResponseInnerMovie> directedMovies = staffService.getAllMoviesByStaffId(staffId, 2);
            responseStaff.setActed(actedMovies);
            responseStaff.setDirected(directedMovies);

            apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(responseStaff);
        } catch (Exception e) {
            e.printStackTrace();
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }

        return apiResponse;
    }


    /**
     * 1.5 根据条件分页获取的影人信息
     * 接口地址:POST /api/v1/staffs
     */
    @ApiOperation("1.5 POST： 根据条件分页获取的影人信息")
    @PostMapping("/api/v1/staffs")
    public ApiResponsePaging<List<ResponseStaff>> staffs(@ApiParam("要查询的演职人员信息") RequestStaff requestStaff) {
        ApiResponsePaging<List<ResponseStaff>> apiResponsePaging = new ApiResponsePaging<>();

        List<ResponseStaff> responseStaffDataList = new ArrayList<>();
        // 如果数据库操作有异常则直接返回500
        try {
            List<Staff> staffList = staffService.getStaffList(requestStaff);

            // 如果未查询到值，则返回空
            for (Staff staff : staffList) {
                ResponseStaff responseStaff = BeanUtil.copyProperties(staff, ResponseStaff.class);

                List<ResponseInnerMovie> actedMovies = staffService.getAllMoviesByStaffId(staff.getId(), 1);
                List<ResponseInnerMovie> directedMovies = staffService.getAllMoviesByStaffId(staff.getId(), 2);
                responseStaff.setActed(actedMovies);
                responseStaff.setDirected(directedMovies);
                responseStaffDataList.add(responseStaff);
            }
            Integer totalRow = responseStaffDataList.size();
            Integer totalPage = Convert.toInt(Math.ceil((double) totalRow / requestStaff.getPagerSize()));
            apiResponsePaging.setCode(200).setSuccess(true).setMessage("请求成功").
                    setPageNumber(requestStaff.getPageNumber()).setPageSize(requestStaff.getPagerSize()).
                    setTotalPage(totalPage).setTotalRow(totalRow).setData(responseStaffDataList);
        } catch (Exception e) {
            e.printStackTrace();
            apiResponsePaging.setCode(500).setSuccess(false).setMessage("请求失败");
        }

        return apiResponsePaging;
    }

    /**
     * 2.5.1 影人信息增加
     * @param staff 要添加的影人信息
     * @return 返回添加结果
     */
    @ApiOperation("2.5.1 POST 影人信息增加")
    @PostMapping("/api/v1/add/staff")
    public ApiResponse<Staff> addStaff(Staff staff) {
        ApiResponse<Staff> apiResponse = new ApiResponse<>();
        try{
            staffService.save(staff);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(staff);
        return apiResponse;
    }

    /**
     * 2.5.2 影人信息修改
     * @param staff 要修改的影人信息
     * @return 返回修改后结果
     */
    @ApiOperation("2.5.2 POST 影人信息修改")
    @PostMapping("/api/v1/update/staff")
    public ApiResponse<Staff> updateStaff(Staff staff) {
        ApiResponse<Staff> apiResponse = new ApiResponse<>();
        try{
            staffService.updateById(staff);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(staff);
        return apiResponse;
    }
}
